var express = require('express');
var path = require('path');
var router = express.Router();
var query = require('./pool');

router.get('*', function (req, res) {
    switch (req.url) {
        case '/addaddress':
            page = "lwbaddaddress.html";
            break;
        case '/gaiaddress':
            page = "lwbgaiaddress.html";
            break;
        case '/administration-1':
            page = "lwbaddressadministration-1.html";
            break;
        case '/administration':
            page = "lwbaddressadministration.html";
            break;
        case '/lwbbill':
            page = "lwbbill.html";
            break;
        case '/bindingmobilephone':
            page = "lwbbindingmobilephone.html";
            break;
        case '/left':
            page = "lwbcenter-left.html";
            break;
        case '/changepassword':
            page = "lwbchangepassword.html";
            break;
        case '/feedback':
            page = "lwbfeedback.html"
            break;
        case '/huanbang1':
            page = "lwbhuanbang1.html"
            break;
        case '/huanbang2':
            page = "lwbhuanbang2.html"
            break;
        case '/huanbang3':
            page = "lwbhuanbang3.html"
            break;
        case '/integraldetails':
            page = "lwbintegraldetails.html"
            break;
        case '/integralorder':
            page = "lwbintegralorder.html"
            break;
        case '/messagefordetails':
            page = "lwbmessagefordetails.html"
            break;
        case '/myaccount':
            page = "lwbmyaccount.html"
            break;
        case '/myfavorite':
            page = "lwbmyfavorite.html"
            break;
        case '/myintegral':
            page = "lwbmyintegral.html"
            break;
        case '/mymessage':
            page = "lwbmymessage.html"
            break;
        case '/myorder':
            page = "lwbmyorder.html"
            break;
        case '/order':
            page = "lwborder.html"
            break;
        case '/orderdetails':
            page = "lwborderdetails.html"
            break;
        case '/ordertoevaluate':
            page = "lwbordertoevaluate.html"
            break;
        case '/personaldata':
            page = "lwbpersonaldata.html"
            break;
        case '/recentbrowse':
            page = "lwbrecentbrowse.html"
            break;
        case '/rechargeable':
            page = "lwbrechargeable.html"
            break;
        case '/rechargeablecard':
            page = "lwbrechargeablecard.html"
            break;
        case '/setapassword':
            page = "lwbsetapassword.html"
            break;
    }
    let pathStr = path.normalize(__dirname + `\/../views/${page}`);
    res.sendFile(pathStr);
});

// 个人资料
router.post('/liulang', async (req, res) => {

    let rows = await query('select * from userinfo where id =' + req.body.id+'');
    // let DATE_FORMAT = await query('SELECT DATE_FORMAT(birdthDay, "%Y-%m-%d") AS birthDay from userinfo');
    let help = await query('select * from userinfo where userName ="' + req.body.userName +'"');
    let sos = await query('select * from userinfo where tel ="' + req.body.tel +'"');
    console.log(rows,req.body,req.body.id,'+++++++++++');
    if (rows.length > 0) {
        let json = {
            code: '1',
            result: rows,
            // res: DATE_FORMAT,
            pl: help,
            ll: sos
        };
        res.send(json);
    } else {
        let json = {
            code: '0'
        };
        res.send(json);
    }
})
router.post('/llang', async (req, res) => {
    console.log(req.body,'==================');
    if (req.body) {
        let sql = 'insert into userinfo set ?';
        let rows = await query(sql,req.body, (error, results,fields) => {
            if(error) throw error;
        });
        let json = {
            code: '100'
        };
        res.send(json);
    }else {
        let json = {
            code: 0
        };
        res.send(json);
    }
});

// 我的账户
router.post('/liul', async (req, res) => {

    let rows = await query('select * from userinfo where id =' + req.body.id+'');
    // let DATE_FORMAT = await query('SELECT DATE_FORMAT(birdthDay, "%Y-%m-%d") AS birthDay from userinfo');
    let help = await query('select * from userinfo where userName ="' + req.body.userName +'"');
    let sos = await query('select * from userinfo where jifen ="' + req.body.jifen +'"');
    let wc = await query('select * from userinfo where money ="' + req.body.money +'"');
    console.log(rows,req.body,req.body.id,'+++++++++++');
    if (rows.length > 0) {
        let json = {
            code: '1',
            result: rows,
            // res: DATE_FORMAT,
            pl: help,
            ll: sos

        };
        res.send(json);
    } else {
        let json = {
            code: '0'
        };
        res.send(json);
    }
})

// 添加地址
router.post('/liuge', async (req, res) => {
    console.log(req.body,'==================');
    if (req.body) {
        let sql = 'insert into address set ?';
        let rows = await query(sql,req.body, (error, results,fields) => {
            if(error) throw error;
        });
        let json = {
            code: '100'
        };
        res.send(json);
    }else {
        let json = {
            code: 0
        };
        res.send(json);
    }
});

module.exports = router;
